<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布者ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入发布帖子的用户ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="帖子标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入帖子标题" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="帖子内容" prop="content">
        <el-input v-model="queryParams.content" placeholder="请输入帖子内容" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['manage:microposts:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:microposts:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:microposts:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['manage:microposts:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="micropostsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="帖子ID" width="65" align="center" prop="postId" />
      <el-table-column label="发布者ID" width="85" align="center" prop="userId" />
      <el-table-column label="帖子标题" align="center" prop="title" />
      <el-table-column label="帖子内容" align="center" prop="content" />
      <el-table-column label="图片URL1" align="center" prop="imageUrl1" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl1" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="图片URL2" align="center" prop="imageUrl2" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl2" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="图片URL3" align="center" prop="imageUrl3" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl3" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="图片URL4" align="center" prop="imageUrl4" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl4" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="图片URL5" align="center" prop="imageUrl5" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl5" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="视频引用" align="center" prop="videoRef" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:microposts:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:microposts:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改微贴子对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="micropostsRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <!-- 左侧栏 -->
          <el-col :span="12">
            <el-form-item label="发布者ID" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入发布帖子的用户ID" />
            </el-form-item>
            <el-form-item label="帖子标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入帖子标题" />
            </el-form-item>
            <el-form-item label="帖子内容" prop="content">
              <el-input v-model="form.content" placeholder="请输入帖子内容" />
            </el-form-item>
            <el-form-item label="视频引用" prop="videoRef">
              <el-input v-model="form.videoRef" placeholder="请输入视频引用" />
            </el-form-item>
            <el-form-item label="图片1" prop="imageUrl1">
              <image-upload v-model="form.imageUrl1" />
            </el-form-item>
            <el-form-item label="图片2" prop="imageUrl2">
              <image-upload v-model="form.imageUrl2" />
            </el-form-item>
          </el-col>
          <!-- 右侧栏 -->
          <el-col :span="12">
            <el-form-item label="图片3" prop="imageUrl3">
              <image-upload v-model="form.imageUrl3" />
            </el-form-item>
            <el-form-item label="图片4" prop="imageUrl4">
              <image-upload v-model="form.imageUrl4" />
            </el-form-item>
            <el-form-item label="图片5" prop="imageUrl5">
              <image-upload v-model="form.imageUrl5" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer-small">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Microposts">
import { listMicroposts, getMicroposts, delMicroposts, addMicroposts, updateMicroposts } from "@/api/manage/microposts";

const { proxy } = getCurrentInstance();

const micropostsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    title: null,
    content: null,
  },
  rules: {
    userId: [
      { required: true, message: "发布帖子的用户ID不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "帖子标题不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "帖子内容不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询微贴子列表 */
function getList() {
  loading.value = true;
  listMicroposts(queryParams.value).then(response => {
    micropostsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    postId: null,
    userId: null,
    title: null,
    content: null,
    imageUrl1: null,
    imageUrl2: null,
    imageUrl3: null,
    imageUrl4: null,
    imageUrl5: null,
    videoRef: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("micropostsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加微贴子";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _postId = row.postId || ids.value
  getMicroposts(_postId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改微贴子";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["micropostsRef"].validate(valid => {
    if (valid) {
      if (form.value.postId != null) {
        updateMicroposts(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMicroposts(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _postIds = row.postId || ids.value;
  proxy.$modal.confirm('是否确认删除微贴子编号为"' + _postIds + '"的数据项？').then(function () {
    return delMicroposts(_postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/microposts/export', {
    ...queryParams.value
  }, `microposts_${new Date().getTime()}.xlsx`)
}

getList();
</script>
